package com.xoa.controller.AndroidAPI;

import com.alibaba.fastjson.JSONObject;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import com.sun.media.sound.FFT;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("AndroidAPI")
public class AndroidAPIController {
    // artemis网关服务器ip端口
    private static String HOST = "202.102.167.38:443";
    // 秘钥appkey
    private static String APP_KEY = "21339726";
    // 秘钥appSecret
    private static String APP_SECRET = "K0Q51HpByM9OrifTuz5A";

    /**
     * 分页获取监控点资源
     *
     * @param pageNo   当前页码
     * @param pageSize 分页大小
     * @param response
     * @return
     * @throws IOException
     */
    @RequestMapping("reGetCamerasByPage")
    public String reGetCamerasByPage(Integer pageNo, Integer pageSize, HttpServletResponse response) throws IOException {
        String result = GetCamerasByPage(pageNo, pageSize);
        ServletOutputStream out = null;
        OutputStreamWriter ow = null;
        try {
            response.setHeader("content-type", "text/html;charset=utf-8");
            response.setCharacterEncoding("utf-8");
            out = response.getOutputStream();
            ow = new OutputStreamWriter(out, "UTF-8");
        } catch (Exception e) {

        } finally {
            ow.write(result);
            ow.flush();
            ow.close();
        }
        return result;
    }

    /**
     * 获取监控点预览取流URL
     *
     * @param cameraIndexCode 监控点唯一标识
     * @param response
     * @return
     * @throws IOException
     */
    @RequestMapping("getCameraPreviewURL")
    public String getCameraPreviewURL(String cameraIndexCode, HttpServletResponse response) throws IOException {
        String result = getCameraPreviewURL(cameraIndexCode);
        ServletOutputStream out = null;
        OutputStreamWriter ow = null;
        try {
            response.setHeader("content-type", "text/html;charset=utf-8");
            response.setCharacterEncoding("utf-8");
            out = response.getOutputStream();
            ow = new OutputStreamWriter(out, "UTF-8");
        } catch (Exception e) {

        } finally {
            ow.write(result);
            ow.flush();
            ow.close();
        }
        return result;
    }

    /**
     * 查询监控点信息列表
     *
     * @param pageNo     当前页码
     * @param pageSize   分页大小
     * @param cameraName 监控点名称(支持中英文字符，不能包含 ’ / \ : * ? " < > |，支持模糊搜索最大长度：32)
     * @param response   采用流式输出
     * @return
     * @throws IOException
     */
    @RequestMapping("GetCamerasByParams")
    public String GetCamerasByParams(Integer pageNo, Integer pageSize, String cameraName, HttpServletResponse response) throws IOException {
        String result = GetCamerasByParams(pageNo, pageSize, cameraName);
        ServletOutputStream out = null;
        OutputStreamWriter ow = null;
        try {
            response.setHeader("content-type", "text/html;charset=utf-8");
            response.setCharacterEncoding("utf-8");
            out = response.getOutputStream();
            ow = new OutputStreamWriter(out, "UTF-8");
        } catch (Exception e) {

        } finally {
            ow.write(result);
            ow.flush();
            ow.close();
        }
        return result;
    }


    /**
     * 获取监控点信息列表
     *
     * @return
     */
    public static String GetCamerasByPage(Integer pageNo, Integer pageSize) {

        /**
         * STEP1：设置平台参数，根据实际情况,设置host appkey appsecret 三个参数.
         */
        ArtemisConfig.host = HOST;
        // 秘钥Appkey
        ArtemisConfig.appKey = APP_KEY;
        // 秘钥AppSecret
        ArtemisConfig.appSecret = APP_SECRET;
        /**
         * STEP2：设置OpenAPI接口的上下文
         */
        final String ARTEMIS_PATH = "/artemis";

        /**
         * STEP3：设置接口的URI地址
         */
        final String getRootApi = ARTEMIS_PATH + "/api/resource/v1/cameras";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", getRootApi);//根据现场环境部署确认是http还是https
            }
        };

        /**
         * STEP4：设置参数提交方式
         */
        String contentType = "application/json";

        /**
         * STEP5：组装请求参数
         */
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("pageNo", pageNo);
        jsonBody.put("pageSize", pageSize);
        String body = jsonBody.toJSONString();

        /**
         * STEP6：调用接口
         */
        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType);

        //System.out.println("请求路径：" + getRootApi + ",请求参数：" + body + ",返回结果：" + result);

        return result;
    }

    /**获取监控点预览取流URL
     * @param cameraIndexCode
     * @return
     */
    public static String getCameraPreviewURL(String cameraIndexCode) {

        /**
         * STEP1：设置平台参数，根据实际情况,设置host appkey appsecret 三个参数.
         */
        // artemis网关服务器ip端口
        ArtemisConfig.host = HOST;
        // 秘钥Appkey
        ArtemisConfig.appKey = APP_KEY;
        // 秘钥AppSecret
        ArtemisConfig.appSecret = APP_SECRET;

        /**
         * STEP2：设置OpenAPI接口的上下文
         */
        final String ARTEMIS_PATH = "/artemis";

        /**
         * STEP3：设置接口的URI地址
         */
        final String getCamsApi = ARTEMIS_PATH + "/api/video/v1/cameras/previewURLs";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", getCamsApi);//根据现场环境部署确认是http还是https
            }
        };

        /**
         * STEP4：设置参数提交方式
         */
        String contentType = "application/json";

        /**
         * STEP5：组装请求参数
         */
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("cameraIndexCode", cameraIndexCode);
        jsonBody.put("streamType", 0);
        jsonBody.put("protocol", "rtsp");
        jsonBody.put("transmode", 1);
        jsonBody.put("expand", "streamform=ps");
        String body = jsonBody.toJSONString();

        /**
         * STEP6：调用接口
         */
        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType, null);
       // System.out.println(result);
        return result;
    }

    /**
     * 查询监控点信息列表
     *
     * @param pageNo     当前页码
     * @param pageSize   分页大小
     * @param cameraName
     * @return
     */
    public static String GetCamerasByParams(Integer pageNo, Integer pageSize, String cameraName) {

        /**
         * STEP1：设置平台参数，根据实际情况,设置host appkey appsecret 三个参数.
         */
        // artemis网关服务器ip端口
        ArtemisConfig.host = HOST;
        // 秘钥Appkey
        ArtemisConfig.appKey = APP_KEY;
        // 秘钥AppSecret
        ArtemisConfig.appSecret = APP_SECRET;


        /**
         * STEP2：设置OpenAPI接口的上下文
         */
        final String ARTEMIS_PATH = "/artemis";

        /**
         * STEP3：设置接口的URI地址
         */
        final String getRootApi = ARTEMIS_PATH + "/api/resource/v1/camera/advance/cameraList";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", getRootApi);//根据现场环境部署确认是http还是https
            }
        };

        /**
         * STEP4：设置参数提交方式
         */
        String contentType = "application/json";

        /**
         * STEP5：组装请求参数
         */
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("pageNo", pageNo);
        jsonBody.put("pageSize", pageSize);
        jsonBody.put("cameraName", cameraName);
        String body = jsonBody.toJSONString();

        /**
         * STEP6：调用接口
         */
        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType);

       // System.out.println("请求路径：" + getRootApi + ",请求参数：" + body + ",返回结果：" + result);

        return result;
    }

}
